Repository navigation
fix: Keep Bgm stopped when stop is called while play is loading - #4256
Conversation
Bgm.play awaits release, setReleaseMode, setVolume and setSource on the shared player before it calls resume. A stop() called during those awaits stopped the player, and then play went on to resume it, so the music started anyway and isPlaying ended up true. Two overlapping play calls both resumed the player in the same way. play now takes an id, and stop and any later play change it, so a play that has been stopped or replaced returns before resume. isPlaying is set before awaiting resume, so a stop during resume also leaves it false.
There was a problem hiding this comment.
🟡 Changes recommended
Overlapping play calls can let a stale load overwrite the active audio source, and a failed resume can leave isPlaying incorrectly true.
2 open issues
What changed in this PR
Updates Flame Audio’s Bgm playback flow to address #4255, where a pending play() could restart music after stop().
Changes:
- Adds request tracking to skip
resume()for stopped or superseded playback requests. - Sets
isPlayingbefore awaitingresume()so a concurrent stop leaves it false. - Adds four regression tests covering stop during loading or startup, playback after stop, and overlapping playback requests.
| File | Description |
|---|---|
packages/flame_audio/test/bgm_test.dart |
Adds mocked-player tests for playback and stop races. |
packages/flame_audio/lib/bgm.dart |
Adds request tracking, adjusts playback-state timing, and documents cancellation behavior. |
Review effort: Balanced
| if (playId != _playId) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
Good catch, but this one predates this PR: before it the older play also resumed, so the wrong track played outright, and this PR only stops that extra resume. Serialising setup with a lock changes how overlapping play calls behave, so I would rather open it as a follow-up. Happy to add it here instead if you prefer.
There was a problem hiding this comment.
I think it is better to fix this separately.
play sets isPlaying before awaiting resume, so that a stop during resume leaves it false. If resume throws, play completed with the error but left isPlaying true although no music started. Reset it on failure when this play is still the current one, then rethrow.
…me-engine#4261) Follow-up to flame-engine#4256, where this was left to be fixed separately. `Bgm.play()` loads into one shared `AudioPlayer`. Two overlapping calls loaded at the same time, so when the older track finished loading last, its `setSource` reached the player after the newer track had started, and replaced it. Now a lock lets one `play()` at a time release the player and load its source (`synchronized` is already a dependency of `flame_audio`). A `play()` that was replaced or stopped while it waited for the lock loads nothing. `stop()` does not take the lock, so it still takes effect at once. `dispose()` now also moves the play counter on, so a waiting `play()` does not touch a disposed player. The cost: a new track starts to load only after the one before it has finished loading. If that earlier load hangs, the new track waits until `audioplayers` times it out (`AudioPlayer.preparationTimeout`). The other way I saw was to keep loading both at once and load the newest source again after a late one lands, which avoids the wait but restarts the newest track. Testing: five tests in `test/bgm_test.dart` are new or changed. The overlap test now checks which source the player ends up holding, and the others cover a replaced waiting play, `stop()` and `dispose()` while a play waits, and a failed load not blocking the next one. Four of them fail on `main`. The failed-load one passes there, as it guards the new lock. I broke each part of the fix on its own and a test failed each time. The whole `flame_audio` suite passes (10 tests), and `dart format`, `flutter analyze` and cspell are clean. I also ran a throwaway test with a real `AudioPlayer` and a fake platform: on `main` the last source the platform receives is the old track, and with this change it is the new one. Not tested: on a device or in a running game, and on Linux or macOS.
…oading (#4307) Follow-up to #4256 and #4261, for `pause()` and the lifecycle handler. When `pause()` was called, or the app left the foreground, while `Bgm.play()` was still loading its track, `play()` still called `resume()` once the track had loaded, so the music started after the game paused it, or played in the background. Now `pause()` sets a flag that `play()` checks after loading: the track stays loaded and `isPlaying` stays false, and `resume()` starts it. `play()` and `resume()` clear the flag. `didChangeAppLifecycleState` also records whether the app is in the resumed state. A track that finishes loading while it is not sets `isPlaying` but is not started, and the `resumed` handler starts it. For that, the `resumed` handler now restarts a player in `PlayerState.stopped` as well as `paused`, when `isPlaying` is true. In `audioplayers` 6.8.1 the state is set in Dart: `release()` calls `stop()`, which sets `stopped`, and `setSource()` leaves the state alone. So a loaded track that was never started is `stopped` if the app left the foreground before `play()` released the player, and `paused` if it left after. Testing: 8 new tests in `test/bgm_test.dart`. 4 fail on `main` (pause while loading, resume after that pause, and the two background cases). The other 4 guard the new flags (play after a pause, pause and resume both during the load, the app coming back before the load ends, and the app coming back after a `pause()`). I broke each part of the fix on its own (10 changes) and a test failed each time. The whole `flame_audio` suite passes (18 tests), and `dart format`, `flutter analyze` and cspell are clean. I also ran a throwaway test with a real `Bgm` and `AudioPlayer` and a fake platform: on `main` the platform gets `resume` after the pause in all three cases, and with this change it gets `resume` only after `resume()` or the app coming back. Not tested: on a device or in a running game.


Description
Bgm.play()awaitsrelease,setReleaseMode,setVolumeandsetSourcebefore it callsresume(). Astop()during those awaits stopped the player, and thenplay()resumed it, so the music started anyway andisPlayingended uptrue. Two overlappingplay()calls both resumed the player the same way.Now each
play()takes an id from a counter thatstop()and any laterplay()also move on. AftersetSource, aplay()whose id is no longer the latest returns without callingresume().isPlayingis now set before awaitingresume(), so astop()that lands duringresume()also leaves itfalse.I went with the counter rather than a lock (
synchronizedis already a dependency), because with a lockstop()would have to wait for the track to load and start before stopping it, so the music could be heard for a moment.Testing: a new
test/bgm_test.dartwith a mockedAudioPlayerwhosesetSourcewaits until the test completes it. Four tests: stop while play is loading, stop while the music is starting, play after stop, and play while another play is loading. Three of them fail onmainand all pass with this PR (the wholeflame_audiosuite: 2 passed and 3 failed onmain, 5 passed here). I also broke each part of the fix on its own (no id check,stop()not moving the counter,play()not taking a new id,isPlayingset afterresume()again), and a test failed each time.dart format,flutter analyzeforflame_audioand cspell are clean. Not tested: on a device or in a running game, and on Linux or macOS.Checklist
docsand added dartdoc comments with///.examplesordocs.Breaking Change?
Related Issues
Closes #4255